#    Copyright 2012 New Mexico EPSCoR
#
#    This file is part of Teacher's P.E.T.
#
#    Teacher's P.E.T. is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Teacher's P.E.T. is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Teacher's P.E.T..  If not, see <http://www.gnu.org/licenses/>.

from django.shortcuts import render_to_response, RequestContext, Http404, HttpResponseRedirect, get_object_or_404
from django.contrib import messages
from django.core.paginator import EmptyPage, PageNotAnInteger
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType

from apps.forum.models import *
from apps.course.models import Course
from apps.participants.models import CourseParticipant
from apps.course.functions import course_reverse
from apps import files

import logging

logger = logging.getLogger('pet.apps.forum.views')

def view_threads(request, course=None, page=1):
    logger.fxn_entry("course=%s",course)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s'",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_FORUM:
        logger.forced_redirect("User %s banned from viewing forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access denied to this forum. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    thread_list = ForumThread.find(forum=TheForum,keep_queryset=True)
    paginator = EmptyPaginator(thread_list, 20) #Show 20 threads per page

    try:
        threads = paginator.page(page)
    except (PageNotAnInteger,TypeError):
        # If page is not an integer, deliver first page.
        threads = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        threads = paginator.page(paginator.num_pages)

    threads.pages_left = paginator.num_pages - threads.number

    logger.fxn_exit("Returning")
    return render_to_response('forum/forum/list_threads.html', {"forum":TheForum,"page": threads,'ban':Bans[0] if Bans else None}, context_instance = RequestContext(request))

def view_entries(request, course=None, thread_id=None, page=1):
    logger.fxn_entry("course=%s, thread_id=%s",course, thread_id)
    
    if not thread_id:
        logger.forced_redirect("No thread_id given, redirecting to main forum view")
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    TheThread = ForumThread.objects.filter(forum=TheForum,id=thread_id)
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s' thread '%s'",TheForum,TheThread)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_FORUM:
        logger.forced_redirect("User %s banned from viewing forum '%s' thread '%s'",TheCourseParticipant,TheForum,TheThread)
        messages.info(request,"Access denied to this forum");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    if not TheThread:
        logger.forced_redirect("Thread with id '%s' doesn't exist in forum %s, redirecting to main forum view",thread_id,TheForum)
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    else: TheThread = TheThread[0]
    
    if TheThread.is_hidden() and TheCourseParticipant.status != "INSTRUCTOR" and TheCourseParticipant.status != "ASSISTANT":
        logger.forced_redirect("Thread with id '%s' marked hidden, User %s denied access, redirecting to main forum view",thread_id,TheCourseParticipant)
        messages.info(request,"Thread not found")
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    
    entry_list = TheThread.entries.all()
    paginator = EmptyPaginator(entry_list, 20) #Show 20 entries per page

    post = None
    if request.method == "GET":
        if 'post' in request.GET:
            post = request.GET['post']
            if post > 0 and post < entry_list.count():
                page = post/20+1;

    try:
        entries = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        entries = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        entries = paginator.page(paginator.num_pages)

    entries.pages_left = paginator.num_pages - entries.number

    #Figure out if this user 'liked' an entry we are displaying
    Likes = ForumEntryLike.objects.filter(course_participant=TheCourseParticipant)
    for entry in entries.object_list:
        entry.liked = False
        for like in Likes:
            if like.entry == entry: entry.liked = True
            
    logger.fxn_exit("Returning")
    return render_to_response('forum/forum/list_entries.html', {"TheForum":TheForum,"TheThread":TheThread,"page": entries,"Likes":Likes,"ban":Bans[0] if Bans else None}, context_instance = RequestContext(request))

def edit_entry(request,course=None,thread_id=None,entry_number=None,quote_entry_number=None):
    #thread_id = None, entry_number = None => new thread
    #thread_id != None, entry_number = None => new entry
    #thread_id != None, entry_number != None => edit entry
    #thread_id != None, entry_number = None, quote_entry_number != None => quote entry in new entry
    logger.fxn_entry("course=%s,thread_id=%s,entry_number=%s,quote_entry_number=%s",course,thread_id,entry_number,quote_entry_number)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s'",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    TheThread = None
    if thread_id: TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    
    TheEntry = None
    if entry_number and TheThread: TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)

    TheQuotedEntry = None
    if quote_entry_number and TheThread: TheQuotedEntry = ForumEntry.find(thread=TheThread,entry_number=quote_entry_number,flagged_as_deleted=False)
    
    #figure out what the user is trying to do and if they gave us good data
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Ban[0].forum_ban >= ForumBan.BAN_POSTS:
        logger.forced_redirect("User %s banned from starting thread or creating/editing entry in forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    if entry_number and not TheEntry:
        logger.forced_redirect("User %s attempted to edit invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
    if TheEntry and TheEntry.author != TheCourseParticipant:
        logger.forced_redirect("User %s attempted to edit %s's entry %s in thread %s in Forum %s",TheCourseParticipant,TheEntry.author, TheEntry, thread_id,TheForum)
        messages.error(request,"You may only edit your own posts");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    if thread_id and not TheThread:
        logger.forced_redirect("User %s attempted to add a new entry in invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    if quote_entry_number and not TheThread:
        logger.forced_redirect("User %s attempted to add to quote an entry in invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    ThreadForm = None
    EntryForm = None
    if request.method == "POST":
        Error = False
        if not TheThread: ThreadForm = ForumThreadCreateForm(TheForum,request.POST)
        EntryForm = ForumEntryEditForm(request.POST,instance=TheEntry)
        
        if ThreadForm:
            if ThreadForm.is_valid():
                if ThreadForm.has_changed():
                    ThreadForm.instance.forum = TheForum
                    TheThread = ThreadForm.save()
                else: logger.form_unchanged("ThreadForm unchanged")
            else: Error = True; logger.error("ThreadForm invalid - %s",ThreadForm)
        
        NewEntry = False
        if EntryForm:
            if EntryForm.is_valid() and TheThread:
                if EntryForm.has_changed():
                    EntryForm.instance.author = TheCourseParticipant
                    EntryForm.instance.thread = TheThread
                    if not TheEntry: NewEntry = True
                    else: EntryForm.instance.edited_date = datetime.datetime.now()
                    TheEntry = EntryForm.save()
                    if not TheThread.first_entry:
                        TheThread.first_entry = TheEntry
                        TheThread.save()
                    elif NewEntry:
                        TheThread.last_entry = TheEntry
                        TheThread.save()
                else: logger.form_unchanged("EntryForm unchanged")
            else: Error = True; logger.error("EntryForm Invalid or no Thread specified or created (Thread is %s) - %s",TheThread,EntryForm)
            
        if not Error:
            if 'Attach' in request.POST:
                return HttpResponseRedirect(course_reverse('forum_attach_entry',TheCourse,thread_id=TheThread.id,entry_number=TheEntry.entry_number))
            return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id)+"?post=%s"%TheEntry.entry_number)
    else:
        if TheQuotedEntry:
            citation = course_reverse('forum_view_entries',thread_id=TheThread.id) + "?post=" + str(TheQuotedEntry.entry_number)
            EntryForm = ForumEntryEditForm(initial={'entry':"<fieldset><legend>Posted by <a href='" + citation + "'>" + TheQuotedEntry.author.user.username + "</a></legend><blockquote cite='" + citation + "'>" + TheQuotedEntry.entry + "</blockquote></fieldset><br><br>"})
        else:
            EntryForm = ForumEntryEditForm(instance=TheEntry)
        if not TheThread: ThreadForm = ForumThreadCreateForm(TheForum)
    
    logger.fxn_exit("Returning")
    return render_to_response('forum/forum/edit_entry.html', {"TheForum":TheForum, "TheThread":TheThread, "TheEntry": TheEntry, "QuotesEntry":TheQuotedEntry, "ThreadForm":ThreadForm, "EntryForm":EntryForm}, context_instance = RequestContext(request))

def flag_entry(request,course=None,thread_id=None,entry_number=None):
    logger.fxn_entry("course=%s,thread_id=%s,entry_number=%s",course,thread_id,entry_number)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s'",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_FORUM:
        logger.forced_redirect("User %s banned from viewing forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    
    TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)

    if not TheEntry:
        logger.forced_redirect("User %s attempted to flag invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
    if TheEntry.flagged_for_moderator:
        logger.forced_redirect("User %s attempted to flag an entry already flagged in thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Post already flagged");
        return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id)+"?post=" + str(TheEntry.entry_number) + "#post" + str(TheEntry.entry_number))
    if not TheThread:
        logger.forced_redirect("User %s attempted to flag an entry in invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    #Ok, everything looks good, flag the entry
    TheEntry.flagged_for_moderator = True;
    TheEntry.save()
    
    logger.fxn_exit("Flagged entry %s for moderator",TheEntry)
    return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id)+"?post=" + str(TheEntry.entry_number) + "#post" + str(TheEntry.entry_number))

def like_entry(request,course=None,thread_id=None,entry_number=None):
    logger.fxn_entry("course=%s,thread_id=%s,entry_number=%s",course,thread_id,entry_number)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s'",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_FORUM:
        logger.forced_redirect("User %s banned from viewing forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    
    TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)

    #figure out what the user is trying to do and if they gave us good data
    if not TheEntry:
        logger.forced_redirect("User %s attempted to like invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
    if ForumEntryLike.objects.filter(course_participant=TheCourseParticipant,entry=TheEntry):
        logger.forced_redirect("User %s attempted to like an entry already liked in thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Post already flagged");
        return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id)+"?post=" + str(TheEntry.entry_number) + "#post" + str(TheEntry.entry_number))
    if not TheThread:
        logger.forced_redirect("User %s attempted to flag an entry in invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    #Ok, everything looks good, like the entry
    TheLike = ForumEntryLike(course_participant=TheCourseParticipant,entry=TheEntry)
    TheLike.save()
    
    logger.fxn_exit("User %s liked entry %s",TheCourseParticipant,TheEntry)
    return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id)+"?post=" + str(TheEntry.entry_number) + "#post" + str(TheEntry.entry_number))

def attach_entry(request,course=None,thread_id=None,entry_number=None):
    logger.fxn_entry("course=%s,thread_id=%s,entry_number=%s",course,thread_id,entry_number)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)

    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s'",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_POST_ATTACHMENTS:
        logger.forced_redirect("User %s banned managing attachments in forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    #figure out what the user is trying to do and if they gave us good data
    if not TheEntry:
        logger.forced_redirect("User %s attempted to edit invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
    if TheEntry and TheEntry.author != TheCourseParticipant:
        logger.forced_redirect("User %s attempted to edit %s's entry %s in thread %s in Forum %s",TheCourseParticipant,TheEntry.author, TheEntry, thread_id,TheForum)
        messages.error(request,"You may only edit your own posts");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
    if not TheThread:
        logger.forced_redirect("User %s attempted to add a new entry in invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    logger.fxn_exit("Returning")
    return render_to_response('forum/forum/attach_entry.html', {"TheForum":TheForum, "TheThread":TheThread, "TheEntry": TheEntry}, context_instance = RequestContext(request))

def download_attachment(request, course=None, thread_id=None, entry_number=None, filename=None):
    logger.fxn_entry("course %s,thread_id %s,entry_number %s,filename %s",course,thread_id,entry_number,filename)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s' attachment",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_FORUM:
        logger.forced_redirect("User %s banned from downloading attachment in forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    if not TheEntry:
        logger.forced_redirect("User %s attempted to download from invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
        
    if not TheThread:
        logger.forced_redirect("User %s attempted to download from an invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    return files.views.files.serve_file(request,ContentType.objects.get_for_model(TheEntry),TheEntry.pk,filename)

def delete_attachment(request, course=None, thread_id=None, entry_number=None, file_id=None):
    logger.fxn_entry("course %s,thread_id %s,entry_number %s,file_id %s",course,thread_id,entry_number,file_id)
    
    TheCourse = Course.find(course)
    TheCourseParticipant = CourseParticipant.find(TheCourse,request.user)
    TheForum = TheCourse.forum
    TheThread = ForumThread.find(forum=TheForum,id=thread_id)
    TheEntry = ForumEntry.find(thread=TheThread,entry_number=entry_number,flagged_as_deleted=False)
    
    if not TheCourseParticipant:
        logger.forced_redirect("Anonymous user trying to access forum '%s' attachment",TheForum)
        messages.info(request,"Access to this part of the site requires course authentication");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    Bans = ForumBan.objects.filter(forum=TheForum,course_participant=TheCourseParticipant)
    if Bans and Bans[0].forum_ban >= ForumBan.BAN_POST_ATTACHMENTS:
        logger.forced_redirect("User %s banned from starting thread in forum '%s'",TheCourseParticipant,TheForum)
        messages.info(request,"Access Denied. Ban in effect");
        return HttpResponseRedirect(course_reverse('course_index',TheCourse))
    
    if not TheEntry:
        logger.forced_redirect("User %s attempted to download from invalid entry %s in thread %s (id=%s) in Forum %s",TheCourseParticipant,entry_number,TheThread,thread_id,TheForum)
        messages.info(request,"Invalid post specified, please try again");
        if not TheThread: return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))
        else: return HttpResponseRedirect(course_reverse('forum_view_entries',TheCourse,thread_id=TheThread.id))
        
    if not TheThread:
        logger.forced_redirect("User %s attempted to download from an invalid thread %s in Forum %s",TheCourseParticipant,thread_id,TheForum)
        messages.info(request,"Invalid thread specified, please try again");
        return HttpResponseRedirect(course_reverse('forum_view_threads',TheCourse))

    file = get_object_or_404(files.get_model(), pk=file_id)
    file.delete()
    messages.info(request,"Attachment deleted")
    logger.fxn_exit('Returning to attach files after deleting file')
    return HttpResponseRedirect(course_reverse('forum_attach_entry',TheCourse,thread_id=TheThread.id,entry_number=TheEntry.entry_number))
